package com.ruoyi.web.controller.data;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SystemType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.domain.DataMoney;
import com.ruoyi.data.service.IDataMoneyService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消费记录Controller
 *
 * @author zzp
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/data/money")
public class DataMoneyController extends BaseController {
    @Autowired
    private IDataMoneyService dataMoneyService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('data:money:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataMoney dataMoney) {
        startPage();
        List<DataMoney> list = dataMoneyService.selectDataMoneyList(dataMoney);
        return getDataTable(list);
    }

    /**
     * 导出消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('data:money:export')")
    @Log(title = "消费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataMoney dataMoney) {
        List<DataMoney> list = dataMoneyService.selectDataMoneyList(dataMoney);
        ExcelUtil<DataMoney> util = new ExcelUtil<DataMoney>(DataMoney.class);
        util.exportExcel(response, list, "消费记录数据");
    }

    /**
     * 获取消费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:money:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dataMoneyService.selectDataMoneyById(id));
    }

    /**
     * 新增消费记录
     */
    @PreAuthorize("@ss.hasPermi('data:money:add')")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody DataMoney dataMoney) {
        if (StringUtils.isNull(dataMoney.getUserId())) {
            return AjaxResult.error("请选择用户");
        } else {
            SysUser user = userService.selectUserById(dataMoney.getUserId());
            if (StringUtils.isNull(user)) {
                return AjaxResult.error("用户不存在");
            }
            if (dataMoney.getMoneyType().equals(SystemType.IN.getCode())) {
                user.setMoneyAvailable(user.getMoneyAvailable() + dataMoney.getMoney());
            } else {
                if (user.getMoneyAvailable() < dataMoney.getMoney()) {
                    return AjaxResult.error("用户可用金额不足");
                }
                user.setMoneyAvailable(user.getMoneyAvailable() - dataMoney.getMoney());
            }
            userService.updateUserProfile(user);
            dataMoney.setMoneyCurrent(user.getMoneyAvailable());
            dataMoneyService.insertDataMoney(dataMoney);
            return AjaxResult.success();
        }
    }

    /**
     * 修改消费记录
     */
    @PreAuthorize("@ss.hasPermi('data:money:edit')")
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataMoney dataMoney) {
        return toAjax(dataMoneyService.updateDataMoney(dataMoney));
    }

    /**
     * 删除消费记录
     */
    @PreAuthorize("@ss.hasPermi('data:money:remove')")
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dataMoneyService.deleteDataMoneyByIds(ids));
    }
}
